<?php

// @formatter:off
/**
 * @file bydistance.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: bydistance.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/extensions/DuplicatedImages/dimages/bydistance.php $
 */
// @formatter:on

require_once WC_E_DIMAGES_ROOTDIR."/includes/DImages.php";

class ActionControllerDimagesBydistance extends WCActionController {
	const MaxImageSize = 400;
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;

			if(!$wcProfile->allowedTo(WC_PERM_IMAGE_LIST)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You have not enough privileges";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;
		//
		// Loading global pointers.
		global $wcGroup;

		$limiter = "directory";
		$limits = WCFVPager::GetLimitsFor($limiter);

		$manager = DImages::I();

		$distances = array();
		$fcmcs = $manager->comicsByDistance($wcGroup, $distances);
		$comics = array();
		$comicsCount = 1;
		foreach($fcmcs as $key => $comic) {
			if($comicsCount >= $limits["min"] && $comicsCount <= $limits["max"]) {
				$aux = WCFVBoxes::GetComicPreviewAssigns($comic);
				$aux["box_name"] = "{$distances[$key][0]} ({$aux["box_name"]})";
				$comics[] = $aux;
			}
			$comicsCount++;
		}
		$this->_manager->comics_by_distance = $comics;

		$fimgs = $manager->imagesByDistance($wcGroup, $distances);
		$images = array();
		$imagesCount = 1;
		foreach($fimgs as $key => $image) {
			if($imagesCount >= $limits["min"] && $imagesCount <= $limits["max"]) {
				$aux = WCFVBoxes::GetImagePreviewAssigns($image);
				$aux["box_name"] = "{$distances[$key]} ({$aux["box_name"]})";
				$images[] = $aux;
			}
			$imagesCount++;
		}
		$this->_manager->images_by_distance = $images;
		//
		// Pager configuration.
		WCFVPager::GetAssignsIntoManagerFor(max($comicsCount, $imagesCount), $limiter);

		return $ok;
	}
}
